﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using WapCrawler.Post;
using WapCrawlerLib;

namespace WapCrawler.Controls
{
    public partial class ucPostGridView : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void PostLinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            if (Session["topicCrawler"] != null)
            {
                WapCrawlerLib.Post objPost = new WapCrawlerLib.Post();
                int topicCrawlerId = (int)Session["topicCrawler"];
                e.Result = objPost.Filter(topicCrawlerId);
            }
        }

        protected void lbkEdit_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Post/frmEditPost.aspx");
            //LinkButton btnEdit = sender as LinkButton;

            //GridViewRow gvrow = (GridViewRow)btnEdit.NamingContainer;

            ////lblID.Text = PostGridView.DataKeys[gvrow.RowIndex].Value.ToString();
            //lblProductName.Text = gvrow.Cells[2].Text;
            //lblProductNumber.Text = gvrow.Cells[3].Text;
            ////lblPrice.Text = gvrow.Cells[4].Text;

            //this.ctlModalPopupExtender.Show();
        }

        protected void PostGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int postId = (int)PostGridView.DataKeys[e.Row.RowIndex].Value;
                WapCrawlerLib.Post objPost = new WapCrawlerLib.Post();
                objPost = objPost.Get(postId);

                WapDataClassesDataContext dc = new WapDataClassesDataContext();

                var query = from p in dc.TopicPostCrawlers
                            where p.postCrawlerId == objPost.postCrawlerId
                            select p.topicCrawlerId;
                if (query != null)
                {
                    PlaceHolder pl = e.Row.FindControl("PlaceHolder1") as PlaceHolder;
                    foreach (int topicCrawlerId in query)
                    {
                        TopicCrawler objTopic = dc.TopicCrawlers.Single(q => q.topicCrawlerId == topicCrawlerId);
                        HyperLink hyperLinkCat = new HyperLink();
                        hyperLinkCat.Text = objTopic.topicName;
                        pl.Controls.Add(hyperLinkCat);
                        pl.Controls.Add(new LiteralControl("&nbsp;-&nbsp;"));
                    }
                }
            }
        }
    }
}